﻿using System;
using System.IO;
using System.Windows.Forms;
using ClickOnceMaster.WizardConfiguration;
using ClickOnceMaster.Properties;

namespace ClickOnceMaster.Pages
{
    public partial class SourcePage : WizardPage
    {
        #region Instance delegate

        private delegate void ControlTextSetter(string text);

        #endregion

        #region .ctors

        public SourcePage()
        {
            InitializeComponent();
            Text = Resources.SourcePage;
        }

        #endregion

        #region Instance methods

        #region WizardPage overrides

        public override bool ValidatePage()
        {
            bool result = true;

            Validator.Clear();

            if (string.IsNullOrEmpty(WizardSettings.Current.Manifest.SourceFilesPath))
            {
                result = false;
                Validator.SetError(tbManifestPath, Resources.Validation_DirectoryNotFound);
            }
            else
            {
                var directoryInfo = new DirectoryInfo(WizardSettings.Current.Manifest.SourceFilesPath);
                if (directoryInfo.Exists)
                {
                    WizardContext.SourceDirectory = directoryInfo;
                }
                else
                {
                    result = false;
                    Validator.SetError(tbManifestPath, Resources.Validation_DirectoryNotFound);
                }
            }

            return base.ValidatePage() && result;
        }

        protected override void LoadData()
        {
            base.LoadData();

            if (!string.IsNullOrEmpty(WizardSettings.Current.Manifest.SourceFilesPath))
            {
                var directoryInfo = new DirectoryInfo(WizardSettings.Current.Manifest.SourceFilesPath);
                if (directoryInfo.Exists)
                {
                    WizardContext.SourceDirectory = directoryInfo;
                }
            }

            WizardSettings.Current.Manifest.SourceFilesPathChanged += RaiseSourceFilesPathChanged;
        }

        protected override void BindPage()
        {
            base.BindPage();
            RefreshUi();
        }

        #endregion

        #region Event handlers

        private void btLoadManifest_Click(object sender, EventArgs e)
        {
            // Prompt for location of deployment manifest 
            var fbd = new FolderBrowserDialog
                          {
                              SelectedPath = WizardSettings.Current.Manifest.SourceFilesPath
                          };

            if (fbd.ShowDialog() == DialogResult.OK)
            {
                tbManifestPath.Text = fbd.SelectedPath;
            }
        }

        private void tbManifestPath_TextChanged(object sender, EventArgs e)
        {
            var textBox = sender as TextBox;
            if (textBox != null)
            {
                WizardSettings.Current.Manifest.SourceFilesPath = textBox.Text;
            }
        }

        private void RaiseSourceFilesPathChanged(object sender, EventArgs e)
        {
            RefreshUi();
        }

        #endregion

        #region Auxiliary methods

        /// <summary>
        /// Updates UI controls values.
        /// </summary>
        private void RefreshUi()
        {
            SetSourceFilesPath(WizardSettings.Current.Manifest.SourceFilesPath);

            if (!string.IsNullOrEmpty(WizardSettings.Current.Manifest.SourceFilesPath))
            {
                var directoryInfo = new DirectoryInfo(WizardSettings.Current.Manifest.SourceFilesPath);
                if (directoryInfo.Exists)
                {
                    SetFilesCount(directoryInfo.GetFiles().Length.ToString());
                }
                else
                {
                    SetFilesCount(Resources.NoData);
                }
            }
            else
            {
                SetFilesCount(Resources.NoData);
            }
        }

        /// <summary>
        /// Set text for <see cref="tbManifestPath"/> control. Thtread safe.
        /// </summary>
        /// <param name="text"></param>
        private void SetSourceFilesPath(string text)
        {
            if (tbManifestPath.InvokeRequired)
            {
                ControlTextSetter setter = SetSourceFilesPath;
                Invoke(setter, new object[] { text });
            }
            else
            {
                tbManifestPath.Text = text;
            }
        }

        /// <summary>
        /// Set text for <see cref="tbManifestPath"/> control. Thtread safe.
        /// </summary>
        /// <param name="text"></param>
        private void SetFilesCount(string text)
        {
            if (lbFilesCount.InvokeRequired)
            {
                ControlTextSetter setter = SetFilesCount;
                Invoke(setter, new object[] { text });
            }
            else
            {
                lbFilesCount.Text = text;
            }
        }

        #endregion

        #endregion
    }
}